define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/shoporder/index' + location.search,
                    add_url: 'order/shoporder/add',
                    edit_url: 'order/shoporder/edit',
                    del_url: 'order/shoporder/del',
                    multi_url: 'order/shoporder/multi',
                    import_url: 'order/shoporder/import',
                    table: 'shop_order',
                }
            });

            var table = $("#table");


            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#one_t").text(data.extend.one);
                $("#two_t").text(data.extend.two);
                $("#three_t").text(data.extend.three);

            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                singleSelect: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'shop_name', title: __('店铺名称'), operate: 'LIKE', align: 'left',
                            formatter: function (value, row, index, field) {
                                if (value == null) {
                                    return "";
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.shop_name + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "170px"
                                    }
                                };
                            }
                        },
                        {field: 'shop_id', title: __('店铺ID')},
                        {
                            field: 'room_name', title: __('包厢名'), operate: 'LIKE', align: 'left',
                            formatter: function (value, row, index, field) {
                                if (value == null) {
                                    return "";
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.room_name + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "190px"
                                    }
                                };
                            }
                        },
                        {field: 'room_id', title: __('包厢ID')},
                        {field: 'user.mobile', title: __('手机号')},
                        {field: 'order_no', title: __('订单号'), operate: 'LIKE'},
                        {
                            field: 'status',
                            title: __('状态'),
                            searchList: {
                                "paying": __('待支付'),
                                "payed": __('待消费'),
                                "doing": __('消费中'),
                                "done": __('已完成'),
                                "refund": __('已退款')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'amount', title: __('订单金额'), operate: false},
                        {field: 'pay_amount', title: __('实付金额'), operate: false},
                        {
                            field: 'book_start',
                            title: __('开始时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'book_end',
                            title: __('结束时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'list',
                                    title: __('退款'),
                                    text: '退款',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "order/shoporder/add?id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('开启订单'),
                                    text: '开启订单',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    confirm: '是否开启订单',
                                    url: function (row) {
                                        return "order/shoporder/open?id=" + row['id'];
                                    },
                                    refresh: true,
                                },
                                {
                                    name: 'list',
                                    title: __('更改时间'),
                                    text: '更改时间',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "order/shoporder/edit?id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('清除保洁'),
                                    text: '清除保洁',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: function (row) {
                                        return "order/shoporder/clean?id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('关闭订单'),
                                    text: '关闭订单',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    confirm: '是否关闭订单',
                                    url: function (row) {
                                        return "order/shoporder/close?id=" + row['id'];
                                    },
                                    refresh: true,
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
